import { getUserInfoAPI, getUserProfileAPI, updatePhotoAPI, updateProfileAPI } from '@/api/user';
import { AppDispatch } from '..';
import { ProfileType } from '@/store/reducer/user';

/** 获取用户信息的Action */
export const getUserInfoAction = () => {
  return async (dispatch: AppDispatch) => {
    const res = await getUserInfoAPI();
    dispatch({ type: 'user/saveUserInfo', payload: res.data });
  };
};

/** 获取用户个人资料的Action */
export const getUserProfileAction = () => {
  return async (dispatch: AppDispatch) => {
    const res = await getUserProfileAPI();
    dispatch({ type: 'user/saveProfile', payload: res.data });
  };
};

/** 更新头像的Action */
export const updatePhotoAction = (data: FormData) => {
  return async (dispatch: AppDispatch) => {
    const res = await updatePhotoAPI(data);
    dispatch({ type: 'user/updateProfile', payload: res.data });
  };
};

/** 更新个人资料的Action */
export const updateProfileAction = (data: Partial<ProfileType>) => {
  return async (dispatch: AppDispatch) => {
    await updateProfileAPI(data);
    // 1. 更新redux
    dispatch({ type: 'user/updateProfile', payload: data });
  };
};
